package von.seiji.cn.test;

import com.beust.jcommander.internal.Lists;
import org.junit.Test;
import org.opencv.core.Point;

import java.util.HashSet;
import java.util.List;
import java.util.TreeMap;
import java.util.stream.Collectors;

public class 点坐标 {


    @Test
    public void test27() {
        long a = 3, b = 0, c = -9;
        System.out.println(a >>> 63);//0
        System.out.println(b >>> 63);//0
        System.out.println(c >>> 63);//1
        System.out.println("----------------------------");//---
        System.out.println(a >> 63);//0
        System.out.println(b >> 63);//0
        System.out.println(c >> 63);//-1
    }

    @Test
    public void test39() {
        System.out.println(1 << 30);
        System.out.println(0x40000000);
        System.out.println(Integer.MAX_VALUE == 1 << 30 + 0x40000000);
        System.out.println(1 << 31 == Integer.MIN_VALUE);
        System.out.println(Integer.toBinaryString(-9));
        System.out.println(Integer.toBinaryString(-9 << 1));
        System.out.println(Integer.toBinaryString(-9 >> 10));
    }


    @Test
    public void test28() {
        System.out.println(Math.round((-0.987458934)));
        System.out.println(-0.0 == 0.0);
        System.out.println(~-3);
        System.out.println(Math.pow(-2, 63) == Long.MIN_VALUE);
        System.out.println(((int) Long.MIN_VALUE));
    }

    //比如下面这段C代码用来判断加法是否溢出。
    /*int OverflowDetect(int n1, int n2){	int t = n1 + n2;	int result = (t ^ n1) & (t ^ n2);	return (result >> (sizeof(int) * 8 - 1)) != 0;}

作者：知乎用户
链接：https://www.zhihu.com/question/27195855/answer/35619420
来源：知乎
著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。*/


    /**
     * 判断p是否在abcd组成的四边形内
     *
     * @param a
     * @param b
     * @param c
     * @param d
     * @param p
     * @return 如果p在四边形内返回true, 否则返回false.
     */
    public static boolean pInQuadrangle(Point a, Point b, Point c, Point d,
                                        Point p) {
        double dTriangle = triangleArea(a, b, p) + triangleArea(b, c, p)
                + triangleArea(c, d, p) + triangleArea(d, a, p);
        double dQuadrangle = triangleArea(a, b, c) + triangleArea(c, d, a);
        return dTriangle == dQuadrangle;
    }

    // 返回三个点组成三角形的面积
    private static double triangleArea(Point a, Point b, Point c) {
        double result = Math.abs((a.x * b.y + b.x * c.y + c.x * a.y - b.x * a.y
                - c.x * b.y - a.x * c.y) / 2.0D);
        return result;
    }

    // 返回三个点组成三角形的面积
    private static double triangleArea1(Point a, Point b, Point c) {
        Point ab = new Point(a.x - b.x, a.y - b.y);
        Point ac = new Point(a.x - c.x, a.y - c.y);
        double result = Math.abs((ab.x * ac.y - ac.x * ab.y) / 2.0D);
        return result;
    }

    public Point vector1(Point another1, Point another2) //得到与另一点之间的距离，平面几何中。点与点的距离为：开根((x1-x2)平方+(y1-y2)平方)
    {
        return new Point(another1.x - another2.x, another1.y - another2.y);
    }

    @Test
    public void test31() {
        Point a = new Point(1, 1);
        Point b = new Point(2, 1);
        Point c = new Point(3, 3);
        Point d = new Point(1, 3);
        Point point = new Point(1, 3);
        boolean b1 = pInQuadrangle(a, b, c, d, point);
       // boolean b2 = pInQuadrangle1(a, b, c, d, point);
        boolean b3 = calc(point, a, b, c, d);
        System.out.println(b1);//
        System.out.println(b3);
    }

    public static int judge(int x) {
        int i = (x >> 31) | (~((~x + 1) >> 31) + 1);
        System.out.print(" " + i);
        return i;
    }

    public static boolean pInQuadrangle1(Point1 a, Point1 b, Point1 c, Point1 d,
                                         Point1 t) {
        int v1 = judge(t.vector(a, b));
        int v2 = judge(t.vector(b, c));
        int v3 = judge(t.vector(c, d));
        int v4 = judge(t.vector(d, a));
//     int i = (x >> 31) | (~((~x + 1) >> 31) + 1);
        // int l = (v1 >> 31) ^ (v2 >> 31) ^ (v3 >> 31) ^ (v4 >> 31);
        //比较绝对值（各个绝对值相加等于相加后的绝对值）
        //
        // return (Math.abs(v1 + v2 + v3 + v4) == Math.abs(v1) +Math.abs(v2) +Math.abs(v3) +Math.abs(v4))  ;
        System.out.println("\n::" + (v1 ^ v2 ^ v3 ^ v4));
        return Math.abs(~(v1 ^ v2 ^ v3 ^ v4)) != 1;
    }

    public boolean calc(Point t, Point... points) //得到与另一点之间的距离，平面几何中。点与点的距离为：开根((x1-x2)平方+(y1-y2)平方)
    {
        double [] _vector = new double [4];
        for (int i = 0; i < points.length; i++) {
            Point vector1 = new Point(points[i].x - t.x, points[i].y - t.y);
            Point vector2 = new Point(points[(i + 1) % 4].x - t.x, points[(i + 1) % 4].y - t.y);
             _vector[i] = vector1.x * vector2.y - vector1.y * vector2.x;
        }
        return (_vector[0]>=0&&_vector[1]>=0&&_vector[2]>=0&&_vector[3]>=0) ||
                (_vector[0]<=0&&_vector[1]<=0&&_vector[2]<=0&&_vector[3]<=0);
    }

    public boolean calc1(Point t, Point... points) //得到与另一点之间的距离，平面几何中。点与点的距离为：开根((x1-x2)平方+(y1-y2)平方)
    {
        int ret = 0;
        for (int i = 0; i < points.length; i++) {
            Point vector1 = new Point(points[i].x - t.x, points[i].y - t.y);
            Point vector2 = new Point(points[(i + 1) % 4].x - t.x, points[(i + 1) % 4].y - t.y);
            double _vector = vector1.x * vector2.y - vector1.y * vector2.x;
          /*  int cal = (_vector >> 31) | (~((~_vector + 1) >> 31) + 1);
            System.out.print(" " + cal);
            ret ^= cal;*///有问题
        }
        System.out.println(" \n" + ret);
        return Math.abs(~ret) != 1;
    }

    @Test
    public void test149() {
        int[] a= {-1,0,1};
        HashSet<Integer> set = new HashSet<>();
        for (int i : a) {
            for (int i1 : a) {
                for (int i2 : a) {
                    for (int i3 : a) {
                        int i4 = i ^ i1 ^ i2 ^ i3;
                        set.add(i + i1 + i2 + i3);
                        System.out.print("  "+ i4);
                    }
                }
            }
        }
        System.out.println("\n"+set);
    }

    @Test
    public void test115() {
        long i = Integer.MAX_VALUE + 1;
        System.out.println(i);
    }

    public static boolean centerJudge(List<Point> points,
                                      Point target) {

        return false;

    }

    @Test
    public void test54() {
        List<User> userList = Lists.newArrayList(
                new User().setId("B").setName("张三"),
                new User().setId("A").setName("李四"),
                new User().setId("C").setName("王五")
        );
        TreeMap<String, String> collect = userList.stream().collect(
                Collectors.toMap(User::getId, User::getName, (n1, n2) -> n1, TreeMap::new)
        );
        System.out.println(collect);
    }

}


class User {
    private String id;
    private String name;

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public User setId(String id) {
        this.id = id;
        return this;
    }

    public User setName(String name) {
        this.name = name;
        return this;
    }
}

class  Point1   // 点类
{
    int x;
    int y;
    public Point1()
    {
        this(0,0);
    }
    public Point1(int x,int y)
    {
        this.x=x;
        this.y=y;
    }
    public double getDistanceFrom(Point another) //得到与另一点之间的距离，平面几何中。点与点的距离为：开根((x1-x2)平方+(y1-y2)平方)
    {
        return Math.sqrt(Math.pow(another.x-this.x,2)+Math.pow(another.y-this.y,2));
    }

    public int vector(Point1 another1, Point1 another2) //得到与另一点之间的距离，平面几何中。点与点的距离为：开根((x1-x2)平方+(y1-y2)平方)
    {
        Point1 point = vector1(this, another1);
        Point1 point2 = vector1(this, another2);
        return point.x * point2.y - point.y * point2.x;
    }

    public Point1 vector1(Point1 another1, Point1 another2) //得到与另一点之间的距离，平面几何中。点与点的距离为：开根((x1-x2)平方+(y1-y2)平方)
    {
        return new Point1(another2.x - another1.x,another2.y - another1.y);
    }
}
